package com.cn.yugan.Engine.Json;

import com.cn.yugan.Engine.Beans.ArticleListInfo;
import com.cn.yugan.Engine.Beans.UserInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * Created by Rover on 2016/12/15 .
 * Email 357319517@qq.com
 */
public class UserInfoJson {
    
    public static ArrayList<UserInfo> json2list(byte[] bytes) {
        try {
            ArrayList<UserInfo> list = new ArrayList<>();
            JSONObject json = new JSONObject(new String(bytes, "UTF-8"));
            boolean success = json.getBoolean("success");
            JSONArray arr = json.getJSONObject("data").getJSONArray("voteList");
            for (int j = 0; j < arr.length(); j++) {
                try {
                    JSONObject obj = arr.getJSONObject(j);
                    UserInfo info = new UserInfo();
                    info.setUid(obj.getString("uid"));
                    info.setUsername(obj.getString("username"));
                    info.setImg(obj.getString("img"));
                    info.setIntro(obj.getString("intro"));
                    list.add(info);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if (list.isEmpty()) {
                //空逻辑
                return null;
            } else {
                return list;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
